import React from 'react';
import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import AlertComponent from '../components/alerts/alert.jsx';
import {openConnectionModal} from '../reducers/modals';
import {setConnectionModalExtensionId} from '../reducers/connection-modal';

class Alert extends React.Component {
    constructor (props) {
        super(props);
        bindAll(this, [
            'handleOnCloseAlert',
            'handleOnReconnect'
        ]);
    }
    handleOnCloseAlert () {
        this.props.onCloseAlert(this.props.content);
    }
    handleOnReconnect () {
        this.props.onOpenConnectionModal(this.props.extensionId);
        this.handleOnCloseAlert();
    }
    render () {
        const {
            content,
            level,
            icon,
            closeButton,

            message,
            extensionName,
            showReconnect,
        } = this.props;

        return (
            <AlertComponent
            content={content}
            icon={icon}
            level={level}
            
            closeButton={closeButton}
            onCloseAlert={this.handleOnCloseAlert}
            
            message={message}
            showReconnect={showReconnect}
            extensionName={extensionName}
            onReconnect={this.handleOnReconnect}
            />
        );
    }
}

const mapStateToProps = () => ({});

const mapDispatchToProps = dispatch => ({
    onOpenConnectionModal: id => {
        dispatch(setConnectionModalExtensionId(id));
        dispatch(openConnectionModal());
    }
});

Alert.propTypes = {
    content: PropTypes.string,
    icon: PropTypes.string,
    level: PropTypes.string,
    
    closeButton: PropTypes.bool,
    onCloseAlert: PropTypes.func,
    
    index: PropTypes.number,
    
    extensionId: PropTypes.string,
    message: PropTypes.string,
    extensionName: PropTypes.string,
    onOpenConnectionModal: PropTypes.func,
    showReconnect: PropTypes.bool
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Alert);
